package Core;

import java.util.Stack;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Александр
 */
public class ClipMashine extends Object
{
	Stack states = new Stack();
	Bounds activeBounds;

	public ClipMashine() {}
	public ClipMashine(Graphics g){ init(g); }

	public Bounds clip(Graphics g, int x, int y, int width, int height)
	{
		Bounds b = new Bounds(x, y, x+width, y+height);
		if(activeBounds != null) b.cut(activeBounds);
		activeBounds = b;
		states.push(b);

		g.setClip(activeBounds.xMin, activeBounds.yMin,
				activeBounds.getWidth(), activeBounds.getHeight());

		return b;
	}

	public Bounds removeTopClip(Graphics g)
	{
		states.pop();
		activeBounds = (Bounds) states.peek();

		g.setClip(activeBounds.xMin, activeBounds.yMin,
				activeBounds.getWidth(), activeBounds.getHeight());

		return activeBounds;
	}

	public Bounds init(Graphics g)
	{
		return clip(g, g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight());
	}
}
